clear; clc;
load('data/recsys/netflix.mat');

para.tol = 1e-1;
para.tau = 1.01;

[row, col, val] = find(data);
idx = randperm(length(val));

traIdx = idx(1:floor(length(val)*0.5));
tstIdx = idx(ceil(length(val)*0.5): end);

clear idx;

traData = sparse(row(traIdx), col(traIdx), val(traIdx));
traData(size(data,1), size(data,2)) = 0;

lambdaMax = topksvd(traData, 1, 5);
gradLambda = lambdaMax*(3/4).^(5:20);

para.maxIter = 5000;

%% start testing
% RMSE = zeros(size(gradLambda));
% for i = 1:length(gradLambda)
%     lambda = gradLambda(i);
%     para.regType = 1;
% 
%     t = tic;
%     para.speedup = 1;
%     [U, S, V, out{1}] = AccSoftImpute( traData', lambda, para );
%     % [U, S, V, out{1}] = APGMatComp( traData', lambda, para );
%     Time = toc(t);
%     RMSE(i) = MatCompRMSE(V, U, S, row(tstIdx), col(tstIdx), val(tstIdx));
% 
%     clear U S V t;
% end
  
para.regType = 1;
% RMSE = zeros(size(gradLambda));
% for i = 1:length(gradLambda)
%     lambda = gradLambda(i);
%     para.maxR = 100;
%      
%     switch(para.regType)
%         case 1
%             theta1 = 1.25*lambda;
%         case 2
%             gradLambda(i) = gradLambda(i)*4;
%             lambda = gradLambda(i);
%             theta1 = sqrt(lambda);
%         case 3
%             theta1 = 2;
%     end
% 
%     t = tic;
%     para.speedup = 1;
%     [U, S, V, out{3,i}] = FastMatComp( traData, lambda, theta1, para );
%     Time(i) = toc(t);
%     RMSE(i) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));
% 
%     clear U S V t;
% end

lambda = 20;
gridTheta = 2.^(2:-1:-2);
RMSE = zeros(size(gridTheta));
for i = 1:length(gridTheta)
    theta1 = gridTheta(i);
    switch(para.regType)
        case 1
            theta1 = theta1*lambda;
            para.maxR = 50;
        case 2
            theta1 = theta1*sqrt(lambda);
            para.maxR = 50;
        case 3
            theta1 = 2^(i - 1);
            para.maxR = 50;
    end

    t = tic;
    para.speedup = 1;
    [U, S, V, out{3,i}] = FastMatComp( traData, lambda, theta1, para );
    Time(i) = toc(t);
    RMSE(i) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

    clear U S V t;
end

% t = tic;
% para.speedup = 0;
% [U, S, V, out{2,i}] = FastMatComp( traData, lambda, theta1, para );
% Time(2,i) = toc(t);
% RMSE(2,i) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));

% t = tic;
% para.speedup = 1;
% [U, S, V, out{3,i}] = FastMatComp( traData, lambda, theta1, para );
% Time(3,i) = toc(t);
% RMSE(3,i) = MatCompRMSE( U, V, S, row(tstIdx), col(tstIdx), val(tstIdx));
% 
% clear U S V t;

% %% GIST
% t = tic;
% [ U, S, V, out{4,i} ] = GISTMatComp( traData, lambda, theta1, para);
% Time(4,i) = toc(t);
% RMSE(4,i) = MatCompRMSE( U, V, S, row(valIdx), col(valIdx), val(valIdx));
% 
% clear U S V t;

%% IRNN
% t = tic;
% [ U, S, V, out{5,i} ] = IRNNMatComp( traData, lambda, theta1, para);
% Time(5,i) = toc(t);
% RMSE(5,i) = MatCompRMSE( U, V, S, row(valIdx), col(valIdx), val(valIdx));
% 
% clear U S V t

clear data i val valIdx tstIdx traIdx col row traData movies Age;


